﻿using System;
using Besture.Common;
using Besture.Common.Helper;

namespace Besture.UI.Admin.Control
{
    public partial class UploadSinglePhoto : System.Web.UI.UserControl
    {
        /// <summary>
        /// DIV ID
        /// </summary>
        public string PhotoDivID = System.Guid.NewGuid().ToString();
        /// <summary>
        /// 上传图片路径
        /// </summary>
        public string FolderPath { get; set; }
        /// <summary>
        /// 是否需要生成缩略图
        /// </summary>
        public bool NeedThumbnail { get; set; }
        /// <summary>
        /// 缩略图路径
        /// </summary>
        public string ThumbnailFolderPath { get; set; }
        /// <summary>
        /// 缩略图宽度
        /// </summary>
        public int ThumbnailWidth { get; set; }
        /// <summary>
        /// 缩略图高度
        /// </summary>
        public int ThumbnailHeight { get; set; }
        /// <summary>
        /// 是否必须上传
        /// </summary>
        public bool Required { get; set; }
        /// <summary>
        /// 必须上传图片
        /// </summary>
        public string RequiredImageUrl = "/Admin/Themes/Images/dispensable.png";
        /// <summary>
        /// 提示文字
        /// </summary>
        public string Tips { get; set; }

        /// <summary>
        /// 图片地址
        /// </summary>
        public string ImageUrl
        {
            get { return txtImageUrl.Text; }
            set { txtImageUrl.Text = value; }
        }

        /// <summary>
        /// 临时图片文件夹
        /// </summary>
        public string folderPathTemp;

        public string InitText = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            /*if (!IsPostBack)
            {*/
                txtImageUrl.Text = ImageUrl;
               
                //必须上传
                if(Required)
                {
                    txtImageUrl.CssClass = "imageRequired";
                    RequiredImageUrl = "/Admin/Themes/Images/indispensable.gif";
                }

                //上传文件夹缺省情况
                if (string.IsNullOrEmpty(FolderPath))
                {
                    FolderPath = Constants.PATH_IMAGE_CONTENT;
                }

                if(NeedThumbnail)
                {
                    folderPathTemp = ThumbnailFolderPath;
                }
                else
                {
                    folderPathTemp = FolderPath;
                }

                
                InitText = InitPhotoDiv();
            //}
        }


        /// <summary>
        /// 初始化图片HTML
        /// </summary>
        /// <returns></returns>
        protected string InitPhotoDiv()
        {
            string text = string.Empty;
            if (!string.IsNullOrEmpty(ImageUrl))
            {
                text = text + ("<div class='element' id='" + PhotoDivID + "_div'>");
                text = text + ("    <div>");
                text = text + ("         <img src='" + folderPathTemp + ImageUrl + "' alt='' />");
                text = text + ("    </div>");
                text = text + ("</div>");
            }
            return text;
        }
    }
}